<template>
  <el-dialog
    :title="!dataForm.recordid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="司机账号" prop="userid">
      <el-input v-model="dataForm.userid" placeholder="司机账号"></el-input>
    </el-form-item>
    <el-form-item label="助手账号" prop="datcherid">
      <el-input v-model="dataForm.datcherid" placeholder="助手姓名"></el-input>
    </el-form-item>
    <el-form-item label="驻厂人员" prop="useridmange">
      <el-input v-model="dataForm.useridmange" placeholder="驻厂人员"></el-input>
    </el-form-item>
    <el-form-item label="违规时间" prop="time">
      <el-input v-model="dataForm.time" placeholder="违规时间"></el-input>
    </el-form-item>
    <el-form-item label="违规地点" prop="place">
      <el-input v-model="dataForm.place" placeholder="违规地点"></el-input>
    </el-form-item>
    <el-form-item label="违规描述" prop="description">
      <el-input v-model="dataForm.description" placeholder="违规描述"></el-input>
    </el-form-item>
    <el-form-item label="违规处罚" prop="penalties">
      <el-input v-model="dataForm.penalties" placeholder="违规处罚"></el-input>
    </el-form-item>
<!--      <el-form-item label="违规照片" prop="photo_url">-->
<!--        <el-input v-model="dataForm.photo_url" placeholder="违规照片"></el-input>-->
<!--      </el-form-item>-->

      <el-form-item label="违规照片">
        <el-upload
          class="upload-demo"
          action="http://120.27.140.32:8080/renren-fast/app/userpic/upload"
          :on-success="handleImageUploadSuccess"
          :before-upload="beforeImageUpload">
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">只能上传jpg/png文件</div>
          <el-input v-if="dataForm.photo_url" v-model="dataForm.photo_url" placeholder="已上传照片URL" readonly></el-input>
        </el-upload>

      </el-form-item>
    </el-form>


    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>

</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          recordid: 0,
          userid: '',
          datcherid: '',
          useridmange: '',
          driverName: '',
          assistantName: '',
          residentName: '',
          time: '',
          place: '',
          description: '',
          penalties: '',
          photo_url: '',
          driverIdcard: ''
        },
        dataRule: {
          userid: [
            { required: true, message: '司机账号不能为空', trigger: 'blur' }
          ],
          useridmange: [
            { required: true, message: '驻厂人员账号不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.recordid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.recordid) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/violationrecords/info/${this.dataForm.recordid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userid = data.violationRecords.userid
                this.dataForm.datcherid = data.violationRecords.datcherid
                this.dataForm.useridmange = data.violationRecords.useridmange
                this.dataForm.driverName = data.violationRecords.driverName
                this.dataForm.assistantName = data.violationRecords.assistantName
                this.dataForm.residentName = data.violationRecords.residentName
                this.dataForm.time = data.violationRecords.time
                this.dataForm.place = data.violationRecords.place
                this.dataForm.description = data.violationRecords.description
                this.dataForm.penalties = data.violationRecords.penalties
                this.dataForm.photo_url = data.violationRecords.photo_url
                this.dataForm.driverIdcard = data.violationRecords.driverIdcard
              }
            })
          }
        })
      },
      handleImageUploadSuccess (response, file, fileList) {
        if (response.code === 0) {
          this.dataForm.photo_url = response.data
          this.$message({
            message: '上传成功',
            type: 'success',
            duration: 1500
          })
        } else {
          this.$message.error(response.msg)
        }
      },
      beforeImageUpload (file) {
        const isJPGorPNG = file.type === 'image/jpeg' || file.type === 'image/png'
        const isLt2M = file.size / 1024 / 1024 < 2

        if (!isJPGorPNG) {
          this.$message.error('上传图片只能是 JPG/PNG 格式!')
        }
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 2MB!')
        }
        return isJPGorPNG && isLt2M
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/PCguanli/violationrecords/${!this.dataForm.recordid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'recordid': this.dataForm.recordid || undefined,
                'userid': this.dataForm.userid,
                'datcherid': this.dataForm.datcherid,
                'useridmange': this.dataForm.useridmange,
                'driverName': this.dataForm.driverName,
                'assistantName': this.dataForm.assistantName,
                'residentName': this.dataForm.residentName,
                'time': this.dataForm.time,
                'place': this.dataForm.place,
                'description': this.dataForm.description,
                'penalties': this.dataForm.penalties,
                'photo_url': this.dataForm.photo_url,
                'driverIdcard': this.dataForm.driverIdcard
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
